import {
    request
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {showToast} from '../../utils/asyncWx.js';
// pages/goods_detail/goods_detail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goods:{},
        isCollect:false,
        tabs:[
            {
                id:0,
                value:"综合",
                isActive:true
            },
            {
                id:1,
                value:"销量",
                isActive:false
            },
            {
                id:2,
                value:"价格",
                isActive:false
            },
        ],
        shopDesc:{
            name:"xxx专卖店",
            head_img:"https://tse1-mm.cn.bing.net/th/id/OIP-C.fR9s9Ys1hzRMIURSfhgBAwAAAA?pid=ImgDet&rs=1",
            background:"",
            authentication:"",
            star:5
        },
        appraiseTotalNum:888,
        appraise:{
            name:"张三",
            time:"刚刚",
            head_img:"https://tse1-mm.cn.bing.net/th/id/OIP-C.fR9s9Ys1hzRMIURSfhgBAwAAAA?pid=ImgDet&rs=1",
            text:"非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢非常喜欢",
            imgs:[
                "https://tse1-mm.cn.bing.net/th/id/OIP-C.fR9s9Ys1hzRMIURSfhgBAwAAAA?pid=ImgDet&rs=1",
                "https://tse1-mm.cn.bing.net/th/id/OIP-C.fR9s9Ys1hzRMIURSfhgBAwAAAA?pid=ImgDet&rs=1",
                "https://tse1-mm.cn.bing.net/th/id/OIP-C.fR9s9Ys1hzRMIURSfhgBAwAAAA?pid=ImgDet&rs=1",
                "https://tse1-mm.cn.bing.net/th/id/OIP-C.fR9s9Ys1hzRMIURSfhgBAwAAAA?pid=ImgDet&rs=1",
                "https://tse1-mm.cn.bing.net/th/id/OIP-C.fR9s9Ys1hzRMIURSfhgBAwAAAA?pid=ImgDet&rs=1"
            ]
        }
    },
    Goods:{},
    
    onShow:function () {
        // 获取当前小程序的页面栈-数组 长度最大为10
        let pages = getCurrentPages();
        // 数组中索引最大的就是当前页面
        let currentPage = pages[pages.length - 1];
        const {goods_id} = currentPage.options;
        this.getGoodsDetail(goods_id);
    },

    // 获取商品的详情数据
    async getGoodsDetail (goods_id) {
        const res = await request({url:'/goods/detail?',data:{goods_id}})
        this.setData({
            goods:{
                goods_name:res.data.message.goods_name,
                goods_price:res.data.message.goods_price,
                // iPhone部分手机不识别 webp格式图片的
                // 让后台工程师修改图片
                goods_introduce:res.data.message.goods_introduce,//.replace(/\.webp/g,'.jpg'),
                pics:res.data.message.pics
            }
        })
        this.Goods = res.data.message

        // 添加到历史浏览中
        let history = wx.getStorageSync('history') || [];
        history = history.filter(v=>v.goods_id!=goods_id);
        this.Goods.num = 1;
        history.unshift(this.Goods);
        wx.setStorageSync('history', history);

        // 获取缓存中的商品收藏数组
        let collect = wx.getStorageSync('collect') || [];
        // 判断该商品是否被收藏过
        let isCollect = collect.some(v=>v.goods_id==goods_id);
        this.setData({isCollect});
    },
    // 点击轮播图放大预览
    handlePreviewImage (options) {
        const {index} = options.currentTarget.dataset;
        //先构造要预览的图片数组
        const urls = this.Goods.pics.map((v) => v.pics_big);
        wx.previewImage({
          urls,
          current:urls[index]
        });
    },
    // 购物车添加
    async handleCartAdd () {
        // 先获取缓存中的购物车数组
        let cart = wx.getStorageSync('cart')||[]
        // 判断商品是否存在购物车数组中
        let index = cart.findIndex(v=>v.goods_id===this.Goods.goods_id)
        if (index===-1) {
            // 不存在
            this.Goods.num = 1
            this.Goods.banMinus = true
            cart.unshift(this.Goods)
        } else {
            // 已经存在执行 num++
            cart[index].num++
        }
        // 把购物车数据重新添加回缓存数据
        wx.setStorageSync('cart', cart)
        // 弹窗提示
        await showToast({
          title: '添加成功',
          icon:'success',
          mask:true
        })
    },
    // 点击商品收藏图标
    async handleCollectGoods () {
        let {isCollect} = this.data;
        const goods = this.Goods;
        // 获取缓存中的商品收藏数组
        let collect = wx.getStorageSync('collect') || [];
        // 判断该商品是否被收藏过
        let index = collect.findIndex(v=>v.goods_id===goods.goods_id);
        // 当index不等于-1时，表示已经收藏过
        if (index !== -1) {
            // 已经存在 在收藏中删除该商品
            isCollect = !isCollect;
            collect.splice(index,1);
            await showToast({title:'取消收藏',icon:'success',mask:true});
        } else {
            // 未收藏该商品
            isCollect = !isCollect;
            goods.num = 1;
            collect.unshift(goods);
            await showToast({title:'收藏成功',icon:'success',mask:true});
        }
        wx.setStorageSync('collect',collect);
        this.setData({isCollect});
    },
    //监听子组件触发的回调函数
    handleTabsItemChange (e) {
        // 获取被点击的标题索引
        const {index} = e.detail
        // 修改原数组 isActive 变为 true
        let {tabs} = this.data
        tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false)
        // 赋值
        this.setData({tabs})
    },
})